package cn.fl.spl.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.ISplCiCfgLogService;
import cn.fl.spl.transform.SplCiCfgLogVOTransform;
import cn.fl.spl.vo.SplCiCfgLogVO;
import cn.fl.system.service.ISysUsrService;
import fl.spl.facade.spl.api.ISplCiCfgLogApiService;
import fl.spl.facade.spl.dto.SplCiCfgLogDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;


/**
 * @Description: 供应商征信配置记录表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class SplCiCfgLogServiceImpl implements ISplCiCfgLogService {

	/**
	 * 自定义 API 自行注入需要的API服务
	 */
	@Autowired
	private ISplCiCfgLogApiService splCiCfgLogApiService;
	@Autowired
	private ISysUsrApiService sysUsrApiService;

	@Override
	public SplCiCfgLogVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
		}
		//TODO 业务逻辑
		SplCiCfgLogDTO dto = null;//this.自定义ApiService.selectSplCiCfgLogDTOById(id);
		return SplCiCfgLogVOTransform.toVO(dto);
	}

	@Override
	public Long add(SplCiCfgLogVO entity) throws BaseException{
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		};
		//return this.splCiCfgLogApiService.add(SplCiCfgLogVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		//return this.splCiCfgLogApiService.removeSplCiCfgLogDTOById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    @Override
	public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		return false;
    }

	@Override
	public boolean modify(SplCiCfgLogVO entity) throws BaseException{
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		};
		//return this.splCiCfgLogApiService.modifySplCiCfgLogDTO(SplCiCfgLogVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		Pager pager = this.splCiCfgLogApiService.searchListPage(page);
		if (null != pager) {
			List<SplCiCfgLogDTO> resultList = (List<SplCiCfgLogDTO>) pager.getResultList();
			List<SplCiCfgLogVO> voList = SplCiCfgLogVOTransform.toVOList(resultList);
			List<Long> usrIdList = new ArrayList<>();
			for (SplCiCfgLogVO splCiCfgLogVO : voList) {
				usrIdList.add(splCiCfgLogVO.getCrtUsrId());
			}

			List<SysUsrDTO> sysUsrDTOS = this.sysUsrApiService.selectSysUserInfoList(usrIdList);
			Map<Long, String> usrMap = new HashMap<>(sysUsrDTOS.size());
			for (SysUsrDTO sysUsrDTO : sysUsrDTOS) {
				usrMap.put(sysUsrDTO.getId(), sysUsrDTO.getRefRlNm());
			}

			for (SplCiCfgLogVO splCiCfgLogVO : voList) {
				splCiCfgLogVO.setCrtUsrNm(usrMap.get(splCiCfgLogVO.getCrtUsrId()));
				if (StringUtils.isNotEmpty(splCiCfgLogVO.getDeclarationB()) && BaseConstant.IS_YESNO_YES == splCiCfgLogVO.getDeclarationB()) {
					splCiCfgLogVO.setDeclarationBNm(BaseConstant.IS_YESNO_YES_NM);
				} else {
					splCiCfgLogVO.setDeclarationBNm(BaseConstant.IS_YESNO_NO_NE);
				}
				if (StringUtils.isNotEmpty(splCiCfgLogVO.getDeclaration()) && BaseConstant.IS_YESNO_YES == splCiCfgLogVO.getDeclaration()) {
					splCiCfgLogVO.setDeclarationNm(BaseConstant.IS_YESNO_YES_NM);
				} else {
					splCiCfgLogVO.setDeclarationNm(BaseConstant.IS_YESNO_NO_NE);
				}
				if (StringUtils.isNotEmpty(splCiCfgLogVO.getEstimateB()) && BaseConstant.IS_YESNO_YES == splCiCfgLogVO.getEstimateB()) {
					splCiCfgLogVO.setEstimateBNm(BaseConstant.IS_YESNO_YES_NM);
				} else {
					splCiCfgLogVO.setEstimateBNm(BaseConstant.IS_YESNO_NO_NE);
				}
				if (StringUtils.isNotEmpty(splCiCfgLogVO.getEstimate()) && BaseConstant.IS_YESNO_YES == splCiCfgLogVO.getEstimate()) {
					splCiCfgLogVO.setEstimateNm(BaseConstant.IS_YESNO_YES_NM);
				} else {
					splCiCfgLogVO.setEstimateNm(BaseConstant.IS_YESNO_NO_NE);
				}
				if (StringUtils.isNotEmpty(splCiCfgLogVO.getIsAuthCrdChkB()) && BaseConstant.IS_YESNO_YES == splCiCfgLogVO.getIsAuthCrdChkB()) {
					splCiCfgLogVO.setIsAuthCrdChkBNm(BaseConstant.IS_YESNO_YES_NM);
				} else {
					splCiCfgLogVO.setIsAuthCrdChkBNm(BaseConstant.IS_YESNO_NO_NE);
				}
				if (StringUtils.isNotEmpty(splCiCfgLogVO.getIsAuthCrdChk()) && BaseConstant.IS_YESNO_YES == splCiCfgLogVO.getIsAuthCrdChk()) {
					splCiCfgLogVO.setIsAuthCrdChkNm(BaseConstant.IS_YESNO_YES_NM);
				} else {
					splCiCfgLogVO.setIsAuthCrdChkNm(BaseConstant.IS_YESNO_NO_NE);
				}
			}
			pager.setResultList(voList);
		}
		return pager;
	}

	@Override
	public Long addAndUpdateOld(SplCiCfgLogVO vo) throws BaseException {
		return this.splCiCfgLogApiService.addAndUpdateOld(SplCiCfgLogVOTransform.toDTO(vo));
	}
}
